package com.leesche.zcsp.node.utils;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class GizwitsApi {
	/**
	 * enterprise_id和produc_key绑定
	 */
	public static String association() {

		ByteBuffer buffer = ByteBuffer.allocate(30);
		buffer.mark();
		buffer.put((byte) 0);
		buffer.put((byte) 0);
		buffer.put((byte) 1);
		byte src[] = { 0, 0, 0, 0 };
		buffer.reset();
		buffer.get(src, 1, 3);
		for (int i = 0; i < 4; ++i) {
			System.out.println(src[i]);
		}

		int offset = 0;
		int value = (int) ( ((src[offset] & 0xFF)<<24)  
			            |((src[offset+1] & 0xFF)<<16)  
			            |((src[offset+2] & 0xFF)<<8)  
			            |(src[offset+3] & 0xFF));  



		String url = "http://enterpriseapi.gizwits.com/v1/products/"
				+ ConfigKit.get("gizwits_produc_key") + "/association";
		JSONObject json = new JSONObject();
		json.put("enterprise_id", ConfigKit.get("gizwits_enterprise_id"));
		json.put("enterprise_secret",
				ConfigKit.get("gizwits_enterprise_secret"));
		json.put("product_secret", ConfigKit.get("gizwits_produc_secret"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Accept", "application/json");
		headers.put("Content-type", "application/json; charset=utf-8");
		headers.put("X-Gizwits-Application-Id", ConfigKit.get("gizwits_app_id"));
		return HttpUtil.post(url, json.toJSONString(), headers);
	}

	public static String getToken() {
		String url = "http://enterpriseapi.gizwits.com/v1/products/"
				+ ConfigKit.get("gizwits_produc_key") + "/access_token";
		JSONObject json = new JSONObject();
		json.put("enterprise_id", ConfigKit.get("gizwits_enterprise_id"));
		json.put("enterprise_secret",
				ConfigKit.get("gizwits_enterprise_secret"));
		json.put("product_secret", ConfigKit.get("gizwits_produc_secret"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Accept", "application/json");
		headers.put("Content-type", "application/json; charset=utf-8");
		headers.put("X-Gizwits-Application-Id", ConfigKit.get("gizwits_app_id"));
		return HttpUtil.post(url, json.toJSONString(), headers);
	}

	public static String sendMsg(ByteBuffer buffer, String token, String did) {
		String url = "http://enterpriseapi.gizwits.com/v1/products/"
				+ ConfigKit.get("gizwits_produc_key") + "/devices/"
				+ did + "/control";
		// "http://enterpriseapi.gizwits.com/v1/products/"+ConfigKit.get("gizwits_produc_key")+"/association";
		
		 
		
		JSONObject json = new JSONObject();

		JSONArray array = new JSONArray();
		json.put("raw", array);

		int limit = buffer.limit();

		byte list[] = buffer.array();
		for (int i = 0; i < limit; ++i) {
			int data = list[i] & 0xff;
			array.add(data);
		}

		System.out.print("json:" + json.toJSONString());

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-type", "application/json; charset=utf-8");
		System.out.println("send token1");
		//headers.put("token", token);
		headers.put("Authorization", "token "+token);
		return HttpUtil.post(url, json.toJSONString(), headers);
	}
}
